﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Management;
using System.Xml;
using System.Diagnostics;
using System.Net;
using Microsoft.Win32;
namespace FEP
{
    public class Helper
    {
        public static string GetFQDN()
        {
            string domainName = System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties().DomainName;
            string hostName = Dns.GetHostName();
            string fqdn = "";
            if (!hostName.Contains(domainName))
                fqdn = hostName + "." + domainName;
            else
                fqdn = hostName;

            return fqdn;
        } 

        public bool BoolConvert(string input)
        {
            bool retval = false;
            try
            {
                retval = Convert.ToBoolean(input);
            }
            catch { }

            return retval;
        }
        public int IntConvert(string input)
        {
            int retval = 0;
            try 
            {
                retval = (int)Convert.ToDouble(input);
            }
            catch { }
            return retval;
        }
        public static string getHostURL()
        {
            string retval = "";
            try
            {

                RegistryKey key = Registry.LocalMachine.OpenSubKey("Software\\Inacom\\FEPClientService");
                retval = key.GetValue("HostURL").ToString();
            }
            catch (Exception ex) { retval = ex.InnerException.ToString(); }

            return retval;

        }
        }
    public class EventHelper
    {
        public string _appName;
        public EventHelper(string AppName)
        {
            _appName = AppName;
        }
        public bool writeEvent(EventLogEntryType EntryType, int EventID, string EventText)
        {
            bool retval = false;
            string log = "Application";
            try
            {
                if (!EventLog.SourceExists(_appName))
                    EventLog.CreateEventSource(_appName, log);
                EventLog.WriteEntry(_appName, EventText, EntryType, EventID);
                retval = true;
            }
            catch { }

            return retval;
        }
        public string GetIP()
        {
            string strHostName = "";
            strHostName = System.Net.Dns.GetHostName();
            IPHostEntry ipEntry = System.Net.Dns.GetHostEntry(strHostName);
            IPAddress[] addr = ipEntry.AddressList;
            return addr[addr.Length - 1].ToString();
        }
       
    }
    public class RegistryHelper
    {
        public string LastActionXML
        {
            get
            {
                return getStringKey("LastActionXML");
            }
            set
            {
                setStringKey("LastActionXML", value);
            }
            
        }
        public string LastActionSuccess
        {
            get
            {
                return getStringKey("LastActionSuccess");
            }
            set 
            {
                setStringKey("LastActionSucess", value);
            }
        }
        public string PolicyXML
        {
            get
            {
                return getStringKey("PolicyXML");
            }
            set
            {
                setStringKey("PolicyXML", value);
            }
        }
        public string HostURL
        {
            get
            {
                return getStringKey("HostURL");
            }
            set
            {
                setStringKey("HostURL", value);
            }
        }
        public RegistryHelper()
        {
            createRegistryEntries();
        }
        public bool createRegistryEntries()
        {
            bool retval = false;
            try
            {
                RegistryKey key = Registry.LocalMachine.OpenSubKey("Software", true);
                key.CreateSubKey("Inacom");
                key = Registry.LocalMachine.OpenSubKey("Software\\Inacom", true);
                key.CreateSubKey("FEPClientService");
                key = Registry.LocalMachine.OpenSubKey("Software\\Inacom\\FEPClientService", true);
                
                
            }
            catch { }

            return retval;
        }
        private string getStringKey(string KeyName)
        {
            string retval = "";
            try
            {
                RegistryKey key = Registry.LocalMachine.OpenSubKey("Software\\Inacom\\FEPClientService");
                retval = key.GetValue(KeyName).ToString();
                
            }
            catch { }
            return retval;
        }
        private bool setStringKey(string KeyName, string Value)
        {
            bool retval = false;
            try
            {
                RegistryKey key = Registry.LocalMachine.OpenSubKey("Software", true);
                key = Registry.LocalMachine.OpenSubKey("Software\\Inacom\\FEPClientService", true);
                key.SetValue(KeyName, Value, RegistryValueKind.String);

                retval = true;
            }
            catch { }
            return retval;
        }
    }
}